import sys
import time
import traceback
sys.path.append('..')

from analysis.alternative_data_analysis import relative_sentiment
from analysis.btc_price_analysis import btc_price_analysis
from analysis.cex_analysis import cex_analysis
from analysis.drawdown_analysis import drawdown_ratio, volume_maxdrawdown_analysis
from analysis.onchain_data_analysis import btc_onchain_data_analysis, btc_valuation_percentile_analysis
from analysis.analysis_weeks import analysis_weeks, analysis_ma_weeks
from dataloader.ohlcvm import update_daily_ohlcvm_all_coins
from dataloader.trade_amount import get_exchanges_detail
from sending_report import sending_report
from tools.logger import logger
import os
from tools.task_monitor import task_to_db, send_error_to_email, check_completion_status_of_tasks


if __name__ == '__main__':
    logger.info("开始weekly_run")
    t00 = time.time()
    # 更新交易所信息
    task_to_db(os.path.basename(__file__), 'get_exchanges_detail')
    try:
        get_exchanges_detail()
        task_to_db(os.path.basename(__file__), 'get_exchanges_detail', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="get_exchanges_detail", message=msg)
        # raise e

    t0 = time.time()
    # 更新周报所需价量数据数据
    task_to_db(os.path.basename(__file__), 'update_daily_ohlcvm_all_coins')
    try:
        update_daily_ohlcvm_all_coins()
        task_to_db(os.path.basename(__file__), 'update_daily_ohlcvm_all_coins', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_daily_ohlcvm_all_coins", message=msg)
        # raise e

    t1 = time.time()
    # 计算舆情相对指数
    logger.info('计算舆情相对指数')
    task_to_db(os.path.basename(__file__), 'relative_sentiment')
    try:
        relative_sentiment()
        task_to_db(os.path.basename(__file__), 'relative_sentiment', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="relative_sentiment", message=msg)
        # raise e

    t2 = time.time()
    # btc价格分析
    logger.info('btc价格分析')
    task_to_db(os.path.basename(__file__), 'btc_price_analysis')
    try:
        btc_price_analysis()
        task_to_db(os.path.basename(__file__), 'btc_price_analysis', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="btc_price_analysis", message=msg)
        # raise e

    t3 = time.time()
    # 交易所数据分析
    logger.info('交易所数据分析')
    task_to_db(os.path.basename(__file__), 'cex_analysis')
    try:
        cex_analysis()
        task_to_db(os.path.basename(__file__), 'cex_analysis', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="cex_analysis", message=msg)
        # raise e

    t4 = time.time()
    # 价格及交易额回撤分析
    logger.info('价格及交易额回撤分析')
    task_to_db(os.path.basename(__file__), 'drawdown_ratio')
    try:
        drawdown_ratio()
        task_to_db(os.path.basename(__file__), 'drawdown_ratio', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="drawdown_ratio", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'volume_maxdrawdown_analysis')
    try:
        volume_maxdrawdown_analysis()
        task_to_db(os.path.basename(__file__), 'volume_maxdrawdown_analysis', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="volume_maxdrawdown_analysis", message=msg)
        # raise e

    t5 = time.time()
    # glassnode指标分析
    logger.info('glassnode指标分析')
    task_to_db(os.path.basename(__file__), 'btc_onchain_data_analysis')
    try:
        btc_onchain_data_analysis()
        task_to_db(os.path.basename(__file__), 'btc_onchain_data_analysis', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="btc_onchain_data_analysis", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'btc_valuation_percentile_analysis')
    try:
        btc_valuation_percentile_analysis()
        task_to_db(os.path.basename(__file__), 'btc_valuation_percentile_analysis', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="btc_valuation_percentile_analysis", message=msg)
        # raise e

    t6 = time.time()
    # 每周全市场概况分析
    logger.info('每周全市场概况分析')
    task_to_db(os.path.basename(__file__), 'analysis_weeks')
    try:
        analysis_weeks()
        task_to_db(os.path.basename(__file__), 'analysis_weeks', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="analysis_weeks", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'analysis_ma_weeks')
    try:
        analysis_ma_weeks()
        task_to_db(os.path.basename(__file__), 'analysis_ma_weeks', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="analysis_ma_weeks", message=msg)
        # raise e

    t7 = time.time()
    # 发送邮件
    logger.info(f"sending_report...")
    task_to_db(os.path.basename(__file__), 'sending_report')
    try:
        sending_report()
        task_to_db(os.path.basename(__file__), 'sending_report', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="sending_report", message=msg)
        # raise e
    t8 = time.time()
    check_completion_status_of_tasks()
    logger.info('各任务用时')
    logger.info(f'更新交易所信息：{t0 - t00}')
    logger.info(f'更新价量数据：{t1 - t0}')
    logger.info(f'计算舆情相对指数:{t2 - t1}')
    logger.info(f'btc价格分析:{t3 - t2}')
    logger.info(f'交易所数据分析:{t4 - t3}')
    logger.info(f'价格及交易额回撤分析:{t5 - t4}')
    logger.info(f'glassnode指标分析:{t6 - t5}')
    logger.info(f'每周全市场概况分析:{t7 - t6}')
    logger.info(f'发送邮件:{t8 - t7}')
    time.sleep(3600)
